#include "gtest/gtest.h"
#include <mockcpp/mokc.h>
#include <mockcpp/mockcpp.hpp>
 
#include <vector>
#include <iostream>
 
#include "topoinfo_struct.h"
#include "log.h"
#include "checker.h"
#include "virtual_topo.h"
#include "testcase_utils.h"
#include "coll_native_executor_base.h"
 
using namespace hccl;

class AllGatherVTest : public testing::Test {
protected:
    static void SetUpTestCase()
    {
        std::cout << "AllGatherVTest set up." << std::endl;
    }
 
    static void TearDownTestCase()
    {
        std::cout << "AllGatherVTest tear down." << std::endl;
    }
    virtual void SetUp()
    {
        const ::testing::TestInfo* const test_info = ::testing::UnitTest::GetInstance()->current_test_info();
        std::string caseName = "analysis_result_" + std::string(test_info->test_case_name()) + "_" + std::string(test_info->name());
        Checker::SetDumpFileName(caseName);
    }
 
    virtual void TearDown()
    {
        Checker::SetDumpFileName("analysis_result");
        GlobalMockObject::verify();
        // 这边每个case执行完成需要清理所有的环境变量，如果有新增的环境变量，需要在这个函数中进行清理
        ClearHcclEnv();
    }
};

TEST_F(AllGatherVTest, all_gather_v_910B_opbase_varying_size_1)
{
    RankTable_t ranktable;
    RankTable_For_LLT gen;
    TopoMeta topoMeta;
    gen.GenTopoMeta(topoMeta, 1, 1, 4);
    gen.GenRankTable(topoMeta, ranktable);
 
    vector<u64> counts {1000000, 2000000, 3000000, 4000000};
    vector<u64> displs {0, 1000000, 3000000, 6000000};
 
    TestOpParam testOpParam;
    testOpParam.opType = HcclCMDType::HCCL_CMD_ALLGATHER_V;
    testOpParam.tag = "AllGatherV";
    testOpParam.opMode = OPBASE;
    testOpParam.devtype = DevType::DEV_TYPE_910B;
    testOpParam.VDataDes.counts = counts;
    testOpParam.VDataDes.displs = displs;
    testOpParam.VDataDes.dataType = HcclDataType::HCCL_DATA_TYPE_INT32;
    testOpParam.algName = "AllGatherVMeshOpbaseExecutor";
 
    Checker checker;
    HcclResult ret;
    ret = checker.Check(testOpParam, ranktable, topoMeta);
    EXPECT_EQ(ret, HcclResult::HCCL_SUCCESS);
}

TEST_F(AllGatherVTest, all_gather_v_910B_opbase_varying_size_2)
{
    RankTable_t ranktable;
    RankTable_For_LLT gen;
    TopoMeta topoMeta;
    gen.GenTopoMeta(topoMeta, 1, 1, 4);
    gen.GenRankTable(topoMeta, ranktable);
 
    vector<u64> counts {400000, 300000, 200000, 100000};
    vector<u64> displs {0, 400000, 700000, 900000};
 
    TestOpParam testOpParam;
    testOpParam.opType = HcclCMDType::HCCL_CMD_ALLGATHER_V;
    testOpParam.tag = "AllGatherV";
    testOpParam.opMode = OPBASE;
    testOpParam.devtype = DevType::DEV_TYPE_910B;
    testOpParam.VDataDes.counts = counts;
    testOpParam.VDataDes.displs = displs;
    testOpParam.VDataDes.dataType = HcclDataType::HCCL_DATA_TYPE_INT32;
    testOpParam.algName = "AllGatherVMeshOpbaseExecutor";
 
    Checker checker;
    HcclResult ret;
    ret = checker.Check(testOpParam, ranktable, topoMeta);
    EXPECT_EQ(ret, HcclResult::HCCL_SUCCESS);
}

TEST_F(AllGatherVTest, allgather_v_test)
{
    RankTable_t ranktable;
    RankTable_For_LLT gen;
    TopoMeta topoMeta;
    gen.GenTopoMeta(topoMeta, 1, 1, 2);
    gen.GenRankTable(topoMeta, ranktable);
 
    vector<u64> counts {40000, 30000, 20000, 10000};
    vector<u64> displs {0, 40000, 70000, 90000};
 
    TestOpParam testOpParam;
    testOpParam.opType = HcclCMDType::HCCL_CMD_ALLGATHER_V;
    testOpParam.tag = "AllGatherV";
    testOpParam.opMode = OPBASE;
    testOpParam.devtype = DevType::DEV_TYPE_910B;
    testOpParam.VDataDes.counts = counts;
    testOpParam.VDataDes.displs = displs;
    testOpParam.VDataDes.dataType = HcclDataType::HCCL_DATA_TYPE_INT32;
 
    Checker checker;
    HcclResult ret;
    ret = checker.Check(testOpParam, ranktable, topoMeta);
    EXPECT_EQ(ret, HcclResult::HCCL_SUCCESS);
}

TEST_F(AllGatherVTest, allgather_v_AllGatherVMeshOpbaseExecutor_test)
{
    RankTable_t ranktable;
    RankTable_For_LLT gen;
    TopoMeta topoMeta;
    gen.GenTopoMeta(topoMeta, 1, 1, 1);
    gen.GenRankTable(topoMeta, ranktable);
 
    vector<u64> counts {400000, 300000, 200000, 100000};
    vector<u64> displs {0, 400000, 700000, 900000};
 
    TestOpParam testOpParam;
    testOpParam.opType = HcclCMDType::HCCL_CMD_ALLGATHER_V;
    testOpParam.tag = "AllGatherV";
    testOpParam.opMode = OPBASE;
    testOpParam.devtype = DevType::DEV_TYPE_910B;
    testOpParam.VDataDes.counts = counts;
    testOpParam.VDataDes.displs = displs;
    testOpParam.VDataDes.dataType = HcclDataType::HCCL_DATA_TYPE_INT32;
    testOpParam.algName = "AllGatherVMeshOpbaseExecutor";
 
    Checker checker;
    HcclResult ret;
    ret = checker.Check(testOpParam, ranktable, topoMeta);
    EXPECT_EQ(ret, HcclResult::HCCL_SUCCESS);
}

TEST_F(AllGatherVTest, all_gather_v_910B_opbase_varying_size_3)
{
    RankTable_t ranktable;
    RankTable_For_LLT gen;
    TopoMeta topoMeta;
    gen.GenTopoMeta(topoMeta, 1, 1, 4);
    gen.GenRankTable(topoMeta, ranktable);
 
    vector<u64> counts {200000, 400000, 600000, 800000};
    vector<u64> displs {0, 200000, 600000, 1200000};
 
    TestOpParam testOpParam;
    testOpParam.opType = HcclCMDType::HCCL_CMD_ALLGATHER_V;
    testOpParam.tag = "AllGatherV";
    testOpParam.opMode = OPBASE;
    testOpParam.devtype = DevType::DEV_TYPE_910B;
    testOpParam.VDataDes.counts = counts;
    testOpParam.VDataDes.displs = displs;
    testOpParam.VDataDes.dataType = HcclDataType::HCCL_DATA_TYPE_INT32;
    testOpParam.algName = "AllGatherVMeshOpbaseExecutor";
 
    Checker checker;
    HcclResult ret;
    ret = checker.Check(testOpParam, ranktable, topoMeta);
    EXPECT_EQ(ret, HcclResult::HCCL_SUCCESS);
}

TEST_F(AllGatherVTest, all_gather_v_910B_opbase_varying_size_4)
{
    RankTable_t ranktable;
    RankTable_For_LLT gen;
    TopoMeta topoMeta;
    gen.GenTopoMeta(topoMeta, 1, 1, 4);
    gen.GenRankTable(topoMeta, ranktable);
 
    vector<u64> counts {100000, 300000, 600000, 1200000};
    vector<u64> displs {0, 100000, 400000, 1000000};
 
    TestOpParam testOpParam;
    testOpParam.opType = HcclCMDType::HCCL_CMD_ALLGATHER_V;
    testOpParam.tag = "AllGatherV";
    testOpParam.opMode = OPBASE;
    testOpParam.devtype = DevType::DEV_TYPE_910B;
    testOpParam.VDataDes.counts = counts;
    testOpParam.VDataDes.displs = displs;
    testOpParam.VDataDes.dataType = HcclDataType::HCCL_DATA_TYPE_INT32;
    testOpParam.algName = "AllGatherVMeshOpbaseExecutor";
 
    Checker checker;
    HcclResult ret;
    ret = checker.Check(testOpParam, ranktable, topoMeta);
    EXPECT_EQ(ret, HcclResult::HCCL_SUCCESS);
}

TEST_F(AllGatherVTest, all_gather_v_910B_opbase_varying_size_5)
{
    RankTable_t ranktable;
    RankTable_For_LLT gen;
    TopoMeta topoMeta;
    gen.GenTopoMeta(topoMeta, 1, 1, 5);
    gen.GenRankTable(topoMeta, ranktable);
 
    vector<u64> counts {10000, 20000, 30000, 20000, 10000};
    vector<u64> displs {0};
    for (auto i = 1; i < counts.size(); ++i) {
            displs.emplace_back(displs[i-1] + counts[i-1]);
    }

    TestOpParam testOpParam;
    testOpParam.opType = HcclCMDType::HCCL_CMD_ALLGATHER_V;
    testOpParam.tag = "AllGatherV";
    testOpParam.opMode = OPBASE;
    testOpParam.devtype = DevType::DEV_TYPE_910B;
    testOpParam.VDataDes.counts = counts;
    testOpParam.VDataDes.displs = displs;
    testOpParam.VDataDes.dataType = HcclDataType::HCCL_DATA_TYPE_INT32;
    testOpParam.algName = "AllGatherVMeshOpbaseExecutor";
 
    Checker checker;
    HcclResult ret;
    ret = checker.Check(testOpParam, ranktable, topoMeta);
    EXPECT_EQ(ret, HcclResult::HCCL_SUCCESS);
}

TEST_F(AllGatherVTest, all_gather_v_910B_opbase_varying_size_6)
{
    RankTable_t ranktable;
    RankTable_For_LLT gen;
    TopoMeta topoMeta;
    gen.GenTopoMeta(topoMeta, 1, 1, 6);
    gen.GenRankTable(topoMeta, ranktable);
 
    vector<u64> counts {10000, 20000, 30000, 30000, 20000, 10000};
    vector<u64> displs {0};
    for (auto i = 1; i < counts.size(); ++i) {
            displs.emplace_back(displs[i-1] + counts[i-1]);
    }

    TestOpParam testOpParam;
    testOpParam.opType = HcclCMDType::HCCL_CMD_ALLGATHER_V;
    testOpParam.tag = "AllGatherV";
    testOpParam.opMode = OPBASE;
    testOpParam.devtype = DevType::DEV_TYPE_910B;
    testOpParam.VDataDes.counts = counts;
    testOpParam.VDataDes.displs = displs;
    testOpParam.VDataDes.dataType = HcclDataType::HCCL_DATA_TYPE_INT32;
    testOpParam.algName = "AllGatherVMeshOpbaseExecutor";
 
    Checker checker;
    HcclResult ret;
    ret = checker.Check(testOpParam, ranktable, topoMeta);
    EXPECT_EQ(ret, HcclResult::HCCL_SUCCESS);
}

TEST_F(AllGatherVTest, all_gather_v_910B_opbase_varying_size_7)
{
    RankTable_t ranktable;
    RankTable_For_LLT gen;
    TopoMeta topoMeta;
    gen.GenTopoMeta(topoMeta, 1, 1, 7);
    gen.GenRankTable(topoMeta, ranktable);
 
    vector<u64> counts {10000, 20000, 30000, 40000, 30000, 20000, 10000};
    vector<u64> displs {0};
    for (auto i = 1; i < counts.size(); ++i) {
            displs.emplace_back(displs[i-1] + counts[i-1]);
    }

    TestOpParam testOpParam;
    testOpParam.opType = HcclCMDType::HCCL_CMD_ALLGATHER_V;
    testOpParam.tag = "AllGatherV";
    testOpParam.opMode = OPBASE;
    testOpParam.devtype = DevType::DEV_TYPE_910B;
    testOpParam.VDataDes.counts = counts;
    testOpParam.VDataDes.displs = displs;
    testOpParam.VDataDes.dataType = HcclDataType::HCCL_DATA_TYPE_INT32;
    testOpParam.algName = "AllGatherVMeshOpbaseExecutor";
 
    Checker checker;
    HcclResult ret;
    ret = checker.Check(testOpParam, ranktable, topoMeta);
    EXPECT_EQ(ret, HcclResult::HCCL_SUCCESS);
}

TEST_F(AllGatherVTest, all_gather_v_910B_offload_varying_size_1)
{
    RankTable_t ranktable;
    RankTable_For_LLT gen;
    TopoMeta topoMeta;
    gen.GenTopoMeta(topoMeta, 1, 1, 1);
    gen.GenRankTable(topoMeta, ranktable);

    vector<u64> counts {400000, 300000, 200000, 100000, 100, 200 ,300, 400};
    vector<u64> displs {0, 400000, 700000, 900000, 1000000, 1000100, 1000300, 1000600};

    TestOpParam testOpParam;
    testOpParam.opType = HcclCMDType::HCCL_CMD_ALLGATHER_V;
    testOpParam.tag = "AllGatherV";
    testOpParam.opMode = OFFLOAD;
    testOpParam.devtype = DevType::DEV_TYPE_910B;
    testOpParam.VDataDes.counts = counts;
    testOpParam.VDataDes.displs = displs;
    testOpParam.VDataDes.dataType = HcclDataType::HCCL_DATA_TYPE_INT32;
    testOpParam.algName = "AllGatherVMeshExecutor";

    Checker checker;
    HcclResult ret;
    ret = checker.Check(testOpParam, ranktable, topoMeta);
    EXPECT_EQ(ret, HcclResult::HCCL_SUCCESS);
}

TEST_F(AllGatherVTest, all_gather_v_910B_offload_varying_size_2)
{
    RankTable_t ranktable;
    RankTable_For_LLT gen;
    TopoMeta topoMeta;
    gen.GenTopoMeta(topoMeta, 1, 1, 2);
    gen.GenRankTable(topoMeta, ranktable);

    vector<u64> counts {400000, 300000, 200000, 100000, 100, 200 ,300, 400};
    vector<u64> displs {0, 400000, 700000, 900000, 1000000, 1000100, 1000300, 1000600};

    TestOpParam testOpParam;
    testOpParam.opType = HcclCMDType::HCCL_CMD_ALLGATHER_V;
    testOpParam.tag = "AllGatherV";
    testOpParam.opMode = OFFLOAD;
    testOpParam.devtype = DevType::DEV_TYPE_910B;
    testOpParam.VDataDes.counts = counts;
    testOpParam.VDataDes.displs = displs;
    testOpParam.VDataDes.dataType = HcclDataType::HCCL_DATA_TYPE_INT32;
    testOpParam.algName = "AllGatherVMeshExecutor";

    Checker checker;
    HcclResult ret;
    ret = checker.Check(testOpParam, ranktable, topoMeta);
    EXPECT_EQ(ret, HcclResult::HCCL_SUCCESS);
}

TEST_F(AllGatherVTest, all_gather_v_910B_offload_varying_size_3)
{
    RankTable_t ranktable;
    RankTable_For_LLT gen;
    TopoMeta topoMeta;
    gen.GenTopoMeta(topoMeta, 1, 1, 3);
    gen.GenRankTable(topoMeta, ranktable);

    vector<u64> counts {400000, 300000, 200000, 100000, 100, 200 ,300, 400};
    vector<u64> displs {0, 400000, 700000, 900000, 1000000, 1000100, 1000300, 1000600};

    TestOpParam testOpParam;
    testOpParam.opType = HcclCMDType::HCCL_CMD_ALLGATHER_V;
    testOpParam.tag = "AllGatherV";
    testOpParam.opMode = OFFLOAD;
    testOpParam.devtype = DevType::DEV_TYPE_910B;
    testOpParam.VDataDes.counts = counts;
    testOpParam.VDataDes.displs = displs;
    testOpParam.VDataDes.dataType = HcclDataType::HCCL_DATA_TYPE_INT32;
    testOpParam.algName = "AllGatherVMeshExecutor";

    Checker checker;
    HcclResult ret;
    ret = checker.Check(testOpParam, ranktable, topoMeta);
    EXPECT_EQ(ret, HcclResult::HCCL_SUCCESS);
}

TEST_F(AllGatherVTest, all_gather_v_910B_offload_varying_size_4)
{
    RankTable_t ranktable;
    RankTable_For_LLT gen;
    TopoMeta topoMeta;
    gen.GenTopoMeta(topoMeta, 1, 1, 4);
    gen.GenRankTable(topoMeta, ranktable);

    vector<u64> counts {400000, 300000, 200000, 100000, 100, 200 ,300, 400};
    vector<u64> displs {0, 400000, 700000, 900000, 1000000, 1000100, 1000300, 1000600};

    TestOpParam testOpParam;
    testOpParam.opType = HcclCMDType::HCCL_CMD_ALLGATHER_V;
    testOpParam.tag = "AllGatherV";
    testOpParam.opMode = OFFLOAD;
    testOpParam.devtype = DevType::DEV_TYPE_910B;
    testOpParam.VDataDes.counts = counts;
    testOpParam.VDataDes.displs = displs;
    testOpParam.VDataDes.dataType = HcclDataType::HCCL_DATA_TYPE_INT32;
    testOpParam.algName = "AllGatherVMeshExecutor";

    Checker checker;
    HcclResult ret;
    ret = checker.Check(testOpParam, ranktable, topoMeta);
    EXPECT_EQ(ret, HcclResult::HCCL_SUCCESS);
}

TEST_F(AllGatherVTest, all_gather_v_910B_offload_varying_size_5)
{
    RankTable_t ranktable;
    RankTable_For_LLT gen;
    TopoMeta topoMeta;
    gen.GenTopoMeta(topoMeta, 1, 1, 5);
    gen.GenRankTable(topoMeta, ranktable);

    vector<u64> counts {400000, 300000, 200000, 100000, 100, 200 ,300, 400};
    vector<u64> displs {0, 400000, 700000, 900000, 1000000, 1000100, 1000300, 1000600};

    TestOpParam testOpParam;
    testOpParam.opType = HcclCMDType::HCCL_CMD_ALLGATHER_V;
    testOpParam.tag = "AllGatherV";
    testOpParam.opMode = OFFLOAD;
    testOpParam.devtype = DevType::DEV_TYPE_910B;
    testOpParam.VDataDes.counts = counts;
    testOpParam.VDataDes.displs = displs;
    testOpParam.VDataDes.dataType = HcclDataType::HCCL_DATA_TYPE_INT32;
    testOpParam.algName = "AllGatherVMeshExecutor";

    Checker checker;
    HcclResult ret;
    ret = checker.Check(testOpParam, ranktable, topoMeta);
    EXPECT_EQ(ret, HcclResult::HCCL_SUCCESS);
}

TEST_F(AllGatherVTest, all_gather_v_910B_offload_varying_size_6)
{
    RankTable_t ranktable;
    RankTable_For_LLT gen;
    TopoMeta topoMeta;
    gen.GenTopoMeta(topoMeta, 1, 1, 6);
    gen.GenRankTable(topoMeta, ranktable);

    vector<u64> counts {400000, 300000, 200000, 100000, 100, 200 ,300, 400};
    vector<u64> displs {0, 400000, 700000, 900000, 1000000, 1000100, 1000300, 1000600};

    TestOpParam testOpParam;
    testOpParam.opType = HcclCMDType::HCCL_CMD_ALLGATHER_V;
    testOpParam.tag = "AllGatherV";
    testOpParam.opMode = OFFLOAD;
    testOpParam.devtype = DevType::DEV_TYPE_910B;
    testOpParam.VDataDes.counts = counts;
    testOpParam.VDataDes.displs = displs;
    testOpParam.VDataDes.dataType = HcclDataType::HCCL_DATA_TYPE_INT32;
    testOpParam.algName = "AllGatherVMeshExecutor";

    Checker checker;
    HcclResult ret;
    ret = checker.Check(testOpParam, ranktable, topoMeta);
    EXPECT_EQ(ret, HcclResult::HCCL_SUCCESS);
}

TEST_F(AllGatherVTest, all_gather_v_910B_offload_varying_size_7)
{
    RankTable_t ranktable;
    RankTable_For_LLT gen;
    TopoMeta topoMeta;
    gen.GenTopoMeta(topoMeta, 1, 1, 7);
    gen.GenRankTable(topoMeta, ranktable);

    vector<u64> counts {400000, 300000, 200000, 100000, 100, 200 ,300, 400};
    vector<u64> displs {0, 400000, 700000, 900000, 1000000, 1000100, 1000300, 1000600};

    TestOpParam testOpParam;
    testOpParam.opType = HcclCMDType::HCCL_CMD_ALLGATHER_V;
    testOpParam.tag = "AllGatherV";
    testOpParam.opMode = OFFLOAD;
    testOpParam.devtype = DevType::DEV_TYPE_910B;
    testOpParam.VDataDes.counts = counts;
    testOpParam.VDataDes.displs = displs;
    testOpParam.VDataDes.dataType = HcclDataType::HCCL_DATA_TYPE_INT32;
    testOpParam.algName = "AllGatherVMeshExecutor";

    Checker checker;
    HcclResult ret;
    ret = checker.Check(testOpParam, ranktable, topoMeta);
    EXPECT_EQ(ret, HcclResult::HCCL_SUCCESS);
}

TEST_F(AllGatherVTest, all_gather_v_910B_offload_varying_size_8)
{
    RankTable_t ranktable;
    RankTable_For_LLT gen;
    TopoMeta topoMeta;
    gen.GenTopoMeta(topoMeta, 1, 1, 8);
    gen.GenRankTable(topoMeta, ranktable);

    vector<u64> counts {400000, 300000, 200000, 100000, 100, 200 ,300, 400};
    vector<u64> displs {0, 400000, 700000, 900000, 1000000, 1000100, 1000300, 1000600};

    TestOpParam testOpParam;
    testOpParam.opType = HcclCMDType::HCCL_CMD_ALLGATHER_V;
    testOpParam.tag = "AllGatherV";
    testOpParam.opMode = OFFLOAD;
    testOpParam.devtype = DevType::DEV_TYPE_910B;
    testOpParam.VDataDes.counts = counts;
    testOpParam.VDataDes.displs = displs;
    testOpParam.VDataDes.dataType = HcclDataType::HCCL_DATA_TYPE_INT32;
    testOpParam.algName = "AllGatherVMeshExecutor";

    Checker checker;
    HcclResult ret;
    ret = checker.Check(testOpParam, ranktable, topoMeta);
    EXPECT_EQ(ret, HcclResult::HCCL_SUCCESS);
}
